package cn.aishua.ui.setting;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import cn.aishua.R;
import cn.aishua.util.SharedPrefConst;

public class PictureSettingActivity
    extends Activity
{
    private SharedPreferences sharedPref;

    private CheckBox cb;

    // page header widgets
    private ImageView ivLeftBtn;
    private ImageView ivRightBtn;
    private TextView tvTitile;

    private void initPageHeader()
    {
        // left button
        ivLeftBtn = (ImageView) findViewById( R.id.page_header_iv_left_btn );
        ivLeftBtn.setImageResource( R.drawable.btn_back );
        ivLeftBtn.setOnClickListener( new View.OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                onBackPressed();
            }
        } );

        // title
        tvTitile = (TextView) findViewById( R.id.page_header_tv_title );
        tvTitile.setText( "图片设置" );

        // right button
        ivRightBtn = (ImageView) findViewById( R.id.page_header_iv_right_btn );
        ivRightBtn.setVisibility( View.GONE );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.setting_picture );
        initPageHeader();
        initiate();
    }

    private void initiate()
    {
        sharedPref = getSharedPreferences( "setting", Context.MODE_PRIVATE );
        cb = (CheckBox) findViewById( R.id.setting_picture_cb );
        cb.setChecked( sharedPref.getBoolean( SharedPrefConst.SHARED_PREF_BOOL_DONW_PIC_UNDER_2G3G, false ) );
        cb.setOnCheckedChangeListener( new OnCheckedChangeListener()
        {

            public void onCheckedChanged( CompoundButton buttonView, boolean isChecked )
            {
                Editor editor = sharedPref.edit();
                if( isChecked )
                {
                    editor.putBoolean( SharedPrefConst.SHARED_PREF_BOOL_DONW_PIC_UNDER_2G3G, true );
                    Toast.makeText( PictureSettingActivity.this, "开启2G/3G网络下载图片", Toast.LENGTH_SHORT ).show();
                }
                else
                {
                    editor.putBoolean( SharedPrefConst.SHARED_PREF_BOOL_DONW_PIC_UNDER_2G3G, false );
                    Toast.makeText( PictureSettingActivity.this, "关闭2G/3G网络下载图片", Toast.LENGTH_SHORT ).show();
                }

                editor.commit();
            }
        } );
    }
}
